<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
use catetree\Catetree;
class Goods extends Controller
{
	public function lst()
	{	
		$typeRes=db('goods')->paginate('10');
		$settitle=settitle('商品');
		$this->assign('settitle',$settitle);
		$this->assign('typeRes',$typeRes);
		return view();
	}
	public function add()
	{	
		

		if(request()->isPost())
		{
			$data=input('post.');
			//验证
  /*  		$validate = validate('type');
    		if(!$validate->check($data)){
			    $this->error($validate->getError());
			}*/
			//处理上传图片,生成缩略图
			/**
			 * [$thumbArr 生成缩略图]
			 * @var [type]
			 * $filename 图片路径
			 * $thumbconfig 生成的缩略图图片大小
			 */
			if($_FILES['og_thumb']['tmp_name']!=false)
			{
				$thumbname='og_thumb';
				$dir=IMG_UPLOADS.DS.'goods'.DS;
				$thumbArr=transthumb($thumbname,$dir,$thumbconfig=array(
				'sm_thumb'=>'300,300',
				'mid_thumb'=>'400,400',
				'big_thumb'=>'500,500',
			));
			}
			dump($thumbArr);
			die;
			$add=db('goods')->insert($data);
			if($add)
			{
				$this->success('添加商品信息成功',url('lst'));
			}else
			{
				$this->error('添加商品信息失败');
			}
		}
		//取出会员级别数据
		$memberLeverRes=db('memberLevel')->select();
		//取出商品栏目
		$categoryRes=db('category')->select();

		$category=new Catetree();
		$categoryRes=$category->cateTree($categoryRes);

		//取出商品的品牌
		$brandRes=db('brand')->where('status',1)->select();
		//取出商品类型
		$typeRes=db('type')->select();
		$settitle=settitle('商品');
		$this->assign(array(
			'settitle'=>$settitle,
			'memberLeverRes'=>$memberLeverRes,
			'typeRes'=>$typeRes,
			'categoryRes'=>$categoryRes,
			'brandRes'=>$brandRes
		));
		return view();
	}

	public function edit()
	{	
		$id=input('id');
		if(request()->isPost())
		{	
			$data=input('post.');
			//验证
/*    		$validate = validate('type');
    		if(!$validate->check($data)){
			    $this->error($validate->getError());
			}*/
			//处理上传图片
			$save=db('goods')->update($data);
			if($save!==false)
			{
				$this->success('修改商品类型成功',url('lst'));
			}else
			{
				$this->error('修改商品类型失败');
			}
		}
		$types=db('goods')->find($id);

		$settitle=settitle('商品类型');
		$this->assign(
			array(
			'types'=>$types,
			'settitle'=>$settitle,
		)
	);
		return view();
	}

	public function del($id)
	{	
		$del=db('goods')->delete($id);
		if($del)
		{
			$this->success('删除商品类型成功',url('lst'));
		}else
		{
			$this->error('删除商品类型失败');
		}
	}
}
